package user

import (
	"net/http"
	"strings"

	"mall/service/recommended/api/internal/logic/user"
	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func CreateUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		req := &types.UserInfo{}
		var err error
		if r.Form["uid"] != nil && len(r.Form["uid"]) > 0 && len(strings.TrimSpace(r.Form["uid"][0])) > 0 {
			req.UserId = r.Form["uid"][0]
		}
		if r.Form["labels"] != nil && len(r.Form["labels"]) > 0 && len(strings.TrimSpace(r.Form["labels"][0])) > 0 {
			req.Labels = strings.Join(r.Form["labels"], ",")
		}
		if r.Form["subscribe"] != nil && len(r.Form["subscribe"]) > 0 && len(strings.TrimSpace(r.Form["subscribe"][0])) > 0 {
			req.Subscribe = r.Form["subscribe"][0]
		}
		if r.Form["comment"] != nil && len(r.Form["comment"]) > 0 && len(strings.TrimSpace(r.Form["comment"][0])) > 0 {
			req.Comment = r.Form["comment"][0]
		}
		l := user.NewCreateUserLogic(r.Context(), svcCtx)
		resp, err := l.CreateUser(req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
